
package common.packet;

import java.awt.Point;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

/**
 * Holds user input info.
 *
 * @author xissburg
 */
public class Input implements Packet
{
    private long timestamp;
    private Point displacement;

    public Input() {
        this(new Point());
    }

    public Input(Point displacement) {
        this(new Point(displacement), 0);
    }

    public Input(Point displacement, long timestamp) {
        this.timestamp = timestamp;
        this.displacement = new Point(displacement);
    }

    public Input(DataInputStream in) throws IOException
    {
        this();
        read(in);
    }

    public Input(Input cd) {
        this(cd.getDisplacement());
    }
    
    public void read(DataInputStream in) throws IOException
    {
        timestamp = in.readLong();
        displacement.x = in.readInt();
        displacement.y = in.readInt();
    }

    public void write(DataOutputStream out) throws IOException
    {
        out.writeUTF(getCode().name());
        out.writeLong(timestamp);
        out.writeInt(displacement.x);
        out.writeInt(displacement.y);
    }

    public Code getCode() {
        return Code.INPUT;
    }

    public Input copy() {
        return new Input(this);
    }

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    public Point getDisplacement() {
        return displacement;
    }

    public void setDisplacement(Point displacement) {
        this.displacement = displacement;
    }
}
